#First ever ruby code for me so be gentle :)
# Takes care of the parsing of itunes xml file and reads it into a litesql database for faster access
#

require "rexml/document"
require "itunes_song.rb"
require "song_database.rb"

class ItunesListener
  attr_reader :key, :value, :is_key, :iSong,:songDB

  def initialize(songDB)
    @is_key = false
    @songDB = songDB
  end

  def tag_start(name, attr)
    if name == 'key'
      @is_key = true
  end

  def text(content)
    if @is_key
      @key = content
    else
      @value = content
    end

  end

  def tag_end(name)
    if @is_key == true
      @is_key = false
    else
      case @key
        when 'Track ID'
          @iSong = ItunesSong.new
          @iSong.songID = value
        when 'Name'
          @iSong.songTitle = value
        when 'Artist'
          @iSong.artist = value
        when 'Album'
          @iSong.album = value
          @songDB.insertSong(@iSong)
          exit
        end
      end
    end
  end

  def method_missing(*arg)
  end

end

#Some random debug code
if __FILE__ == $0
  songDB = SongDatabase.new

  list = ItunesListener.new(songDB)
  source = File.new "/Users/Anth/Music/iTunes/iTunes Music Library.xml";
  REXML::Document.parse_stream(source, list)
end
